package sdk;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import model.Renew;

public class RenewSDK {
    private Client client;

    public RenewSDK(Client client) {
        this.client = client;
    }

    public Map<String, Object> getCompanyExpireDeviceList(Renew.GetCompanyExpireDeviceListReq req) {
        String appId = client.getAppId();
        String appSecret = client.getAppSecret();
        BuildClientSDK buildClient = new BuildClientSDK(appId,appSecret);
        Map<String, Object> getParams = new TreeMap<>();
        getParams.put("page",req.getPageModel().page);  
        getParams.put("limit", req.getPageModel().limit);
        Map<String, Object> getParamsf = buildClient.buildGetParam(getParams);
        return client.get("/open/renew/getCompanyExpireDeviceList", getParamsf);
    }

    public Map<String, Object> createCompanyRenewOrder(Object req) {
        String appId = client.getAppId();
        String appSecret = client.getAppSecret();
        BuildClientSDK buildClient = new BuildClientSDK(appId,appSecret);
        Map<String, Object> combinedParams = new HashMap<>();
                if (req != null) {
            Class<?> clazz = req.getClass();
            for (Field field : clazz.getDeclaredFields()) {
                field.setAccessible(true);
                try {
                    String fieldName = field.getName();
                    Object fieldValue = field.get(req);
                    combinedParams.put(fieldName, fieldValue);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Failed to access field in createCompanyRenewOrder ", e);
                }
            }
        }
        Map<String, Object> postParams = buildClient.buildPostParam(combinedParams);
        return client.post("/open/renew/createCompanyRenewOrder", postParams);
    }

    public Map<String, Object> getCompanyRenewStatus(Renew.GetCompanyRenewStatusReq req) {
        String appId = client.getAppId();
        String appSecret = client.getAppSecret();
        BuildClientSDK buildClient = new BuildClientSDK(appId,appSecret);
        Map<String, Object> getParams = new TreeMap<>();
        getParams.put("out_trade_no",req.getOutTradeNo());
        Map<String, Object> getParamsf = buildClient.buildGetParam(getParams);
        return client.get("/open/renew/getCompanyRenewStatus", getParamsf);
    }
}